import { ApiCall, TsrpcErrorType } from "tsrpc";
import { ReqCheck, ResCheck } from "../shared/api_client/PtlCheck";
import sdk from "../core";
import { ClientManage } from "../core";


export default async function (call: ApiCall<ReqCheck, ResCheck>) {
    let token = call.req._token;
    if (!token) {
        call.error({
            message: 'token 错误',
            type: TsrpcErrorType.ClientError,
        });
        return;
    }

    let data = ClientManage.checkToken(token);
    if (data) {
        if (data.expiredTime < Date.now()) {
            call.error({
                message: '授权已过期 请重新授权',
                type: TsrpcErrorType.ServerError,
            });
            return;
        } else {
            data.expiredTime = Date.now() + 30 * 24 * 60 * 60 * 1000;
        }
    } else if (!ClientManage.waitTokenMap.has(token)) {
        call.error({
            message: '授权已取消',
            type: TsrpcErrorType.ClientError,
        });
        return;
    }
    call.succ({
        judge: !!data
    })
}
